﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class TaskController : BaseAuthController
    {
        /// <summary>
        /// 返回Index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["refID"] = Guid.NewGuid().ToString();
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            ViewData["refID"] = Guid.NewGuid().ToString();
            string innerDay = GetParam("innerDay");
            string title = GetParam("title");
            string status = GetParam("Status");
            TaskBLL bll = new TaskBLL();
            PagedList<EMOA_Task> list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows), p =>
              {
                  return (innerDay.IsNullOrEmpty() || innerDay == "0" || p.FinishTime == null || p.FinishTime.Value.Subtract(DateTime.Now).Days <= Convert.ToInt32(innerDay))
                          && (title.IsNullOrEmpty() || p.Title == title)
                          && (status.IsNullOrEmpty() || p.Status == Convert.ToByte(status));
              });

            return ToJson<EMOA_Task, TaskModels>(list, list.TotalItemCount);
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(TaskModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                TaskBLL bll = new TaskBLL();
                EMOA_Task entity = model.ToEntity<EMOA_Task>();
                entity.TaskID = Guid.Parse(GetParam("refID"));
                entity.CreateBy = CurrentUser.UserID;
                entity.CreateTime = DateTime.Now;
                if (bll.Add(entity))
                {
                    return Content("OK");
                }
                else
                {
                    return Content("增加失败");
                }
                //}
                //else
                //    return Content("验证失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(TaskModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                TaskBLL bll = new TaskBLL();
                EMOA_Task entity = bll.GetEntity(model.TaskID);
                entity = model.ToEntity<EMOA_Task>(entity);
                entity.UpdateBy = CurrentUser.UserID;
                entity.UpdateTime = DateTime.Now;
                if (bll.Update(entity))
                    return Content("OK");
                else
                    return Content("修改失败");
                //}
                //else
                //{
                //    return Content("验证失败");
                //}
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                TaskBLL bll = new TaskBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("删除失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

    }
}